# -*- coding: UTF-8 -*-
import os
import time

sourcePath = "/root/dilute/stl_file"

# 500M
max_file_size = 1024 * 1024 * 500


def scan_files(path, postfix):
    files_list = []
    for root, sub_dirs, files in os.walk(path):
        for special_file in files:
            if special_file.endswith(postfix) and not special_file.endswith("#done.stl") and not special_file.endswith(
                    "#done_upload.stl") and not special_file.endswith("#done_upload_package.stl"):
                files_list.append(os.path.join(root, special_file))
    return files_list


def check_dir(path):
    if not os.path.exists(path):
        os.mkdir(path)


files = scan_files(sourcePath, ".stl")
if len(files) == 0:
    exit()
for file in files:
    print("开始稀释:" + file)
    size = os.path.getsize(file)
    if size >= max_file_size:
        continue
    start_time = time.time()
    split = file.split("/")
    fileName = str(split.pop())
    name = str(fileName.replace('.stl', ''))
    filePath = "/".join(split) + "/"
    check_dir(filePath + "glb5")
    check_dir(filePath + "glb10")
    check_dir(filePath + "glb20")
    os.system(
        "cd /mnt/blender-3.3.1-linux-x64/  && ./blender -b -P /mnt/blender-3.3.1-linux-x64/compress_bq1.py  -t 0 --python-console '" + filePath + "' '" + filePath + "glb5/" + name + "' '5' '" + fileName + "'")
    os.system(
        "cd /mnt/blender-3.3.1-linux-x64/  && ./blender -b -P /mnt/blender-3.3.1-linux-x64/compress_bq1.py  -t 0 --python-console '" + filePath + "' '" + filePath + "glb10/" + name + "' '10' '" + fileName + "'")
    os.system(
        "cd /mnt/blender-3.3.1-linux-x64/  && ./blender -b -P /mnt/blender-3.3.1-linux-x64/compress_bq1.py  -t 0 --python-console '" + filePath + "' '" + filePath + "glb20/" + name + "' '20' '" + fileName + "'")
    os.rename(file, filePath + name + "#done.stl")
    end_time = time.time()
    print("结束稀释," + "耗时:{:.2f}秒".format(end_time - start_time))
exit()
